//
//  CreateLocalNotificationsClocks.m
//  ClockNotifications
//
//  Created by Anders J.R. Petersen on 26/11/13.
//  Copyright (c) 2013 Anders J.R. Petersen. All rights reserved.
//

#import "CreateLocalNotificationsClocks.h"
#import "CreateNotifictaionDates.h"

@interface CreateLocalNotificationsClocks(){
    
}

@end

static CreateNotifictaionDates *creatDates;

static NSString *intervalMinutesKey     =   @"intervalMinutes";
static NSString *notificationsIsOnKey   =   @"notificationsIsOn";
static NSString *notificationArrayKey   =   @"notificationsArray";

@implementation CreateLocalNotificationsClocks

+(void) reInitializeNotifictaions{
    //check if notifications is on/or off / get user data
    BOOL isOn = [[NSUserDefaults standardUserDefaults] boolForKey:notificationsIsOnKey];
    int intervalMinutes = [[NSUserDefaults standardUserDefaults] integerForKey:intervalMinutesKey];
    
    if(isOn){
        [self createNotifications:intervalMinutes];
    }else{
        [self deleteNotifications];
    }
}


+(void) createNotifications: (int) intervalMinutesValue {
    //clear old notifications
    [self deleteNotifications];
    
    //set interval / user data
    [[NSUserDefaults standardUserDefaults] setInteger:intervalMinutesValue forKey:intervalMinutesKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    //create new notifications array
    if(!creatDates){
        creatDates          = [[CreateNotifictaionDates alloc]init];
    }
    
    NSMutableArray *notificationArray   =[[NSMutableArray alloc] init];
    
               // NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
               // [dateFormatter setDateFormat:@"dd. MMMM HH:mm:ss"];
    
    
    for(NSDate *date in [creatDates getNotifictaionDates:intervalMinutesValue]){
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = date;
        localNotification.alertBody = [[NSString alloc] initWithFormat:@"MyrnaMindfulnes Klokken: Det er tid til nærvær. Tryk her og bekræft "];
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
       // localNotification.soundName = UILocalNotificationDefaultSoundName;;
        localNotification.soundName = @"goong.caf";
        //localNotification.soundName = [[NSBundle mainBundle] pathForResource:@"goong2" ofType:@"caf"];
        localNotification.repeatInterval = NSDayCalendarUnit;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        
              //  NSString *strDate = [dateFormatter stringFromDate:[localNotification fireDate]];
              //  NSLog(@"%@ og info: %@", strDate, [localNotification soundName]);
        
        //encrypt data
        NSData *theData=[NSKeyedArchiver archivedDataWithRootObject:localNotification];
        
        [notificationArray addObject:theData];
    }
    
    //set notification array / user data
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:notificationArray] forKey:notificationArrayKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    //set notifications on / user data
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:notificationsIsOnKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(void) deleteNotifications {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    //get notification array / user data
    NSArray *notificationArray =  [[NSUserDefaults standardUserDefaults] arrayForKey:notificationArrayKey];

    for(NSData *theData in notificationArray){
        //decrypt data / and delete notification
        if (theData != nil)
        [[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)[NSKeyedUnarchiver unarchiveObjectWithData:theData]];
    }

    //clear notifications / user data
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:notificationArrayKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    //set notifications off / user data
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:notificationsIsOnKey];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

@end




